﻿#region Using Directives

using System;
using System.Diagnostics;
using Kongo.Properties;

#endregion Using Directives


namespace Kongo
{
    // Helper class that contains common functions for built-in tags.
    internal class BaseTag : Tag
    {
        #region Fields

        private string[] args;
        private bool capture = true;

        #endregion Fields


        #region Methods

        public override bool ChildNode(Node node)
        {
            if (capture)
            {
                // Is this an acceptable end tag?
                BaseTag bt = node as BaseTag;
                if(bt != null && IsEndTag(bt.Args[0]))
                    capture = false; // Stop capturing after this node

                return true;
            }

            return false;
        }


        protected ArgumentException CreateSyntaxException()
        {
            string name = args[0];
            return new ArgumentException(Helpers.Format(Resource.Argument_InvalidTagSyntax, name));
        }


        protected virtual bool IsEndTag(string name)
        {
            // Helper function to make it easier for parent nodes
            // to find their end of scope
            return false;
        }

        #endregion Methods


        #region Properties

        public string[] Args
        {
            get { return args; }
        }

        #endregion Properties


        #region Constructors

        public BaseTag(string[] args)
        {
            Debug.Assert(args != null, "The 'args' argument was null.");
            Debug.Assert(args.Length > 0, "The 'args' argument must contain the tag name.");
            this.args = args;
        }

        #endregion Constructors


        #region Types

        public class BaseTagCreator : ITagCreator
        {
            #region Methods

            public Tag Create(string[] args)
            {
                return new BaseTag(args);
            }

            #endregion Methods
        }

        #endregion Types
    }
}
